﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;

namespace Frw.Protocols.Communications.Tcp
{
    public class TCPClient : IDisposable
    {
        ManualResetEvent doneConnect = new ManualResetEvent(false);
        bool dispose = false;

        public string IPAddress { get; set; }
        public int Port { get; set; }

        private void ConnectCallback(IAsyncResult ar)
        {
            try
            {
                System.Net.Sockets.TcpClient client = (System.Net.Sockets.TcpClient)ar.AsyncState;
                client.EndConnect(ar);
                doneConnect.Set();
            }
            catch (Exception err)
            {
                Console.WriteLine("Could not connect.");
            }
        }

        public void SendMessage(string message)
        {
            System.Net.Sockets.TcpClient tcpClient = new System.Net.Sockets.TcpClient();
            tcpClient.BeginConnect(System.Net.IPAddress.Parse(IPAddress), Port, new AsyncCallback(ConnectCallback), tcpClient);
            doneConnect.WaitOne();

            // Uses the GetStream public method to return the NetworkStream.
            NetworkStream netStream = tcpClient.GetStream();

            if (netStream.CanWrite)
            {
                Byte[] sendBytes = Encoding.UTF8.GetBytes(message); //"Is anybody there?\r\n");
                netStream.Write(sendBytes, 0, sendBytes.Length);
            }
            else
            {
                Console.WriteLine("You cannot write data to this stream.");
                tcpClient.Close();

                // Closing the tcpClient instance does not close the network stream.
                netStream.Close();
                netStream.Dispose();
                return;
            }

            //if (netStream.CanRead)
            //{
            //    // Reads NetworkStream into a byte buffer.
            //    byte[] bytes = new byte[tcpClient.ReceiveBufferSize];

            //    // Read can return anything from 0 to numBytesToRead.
            //    // This method blocks until at least one byte is read.
            //    netStream.Read(bytes, 0, bytes.Length);

            //    // Returns the data received from the host to the console.
            //    string returndata = Encoding.UTF8.GetString(bytes);

            //    Console.WriteLine("RES: " + returndata);
            //    netStream.Close();
            //    netStream.Dispose();
            //}
            //else
            //{
            //    Console.WriteLine("You cannot read data from this stream.");
            //    tcpClient.Close();

            //    // Closing the tcpClient instance does not close the network stream.
            //    netStream.Close();
            //    netStream.Dispose();
            //    return;
            //}
        }

        public void Dispose()
        {
            this.dispose = true;
        }
    }
}
